package com.ms.service.module.fish;

import java.util.Collection;
import java.util.List;

import com.ms.service.module.fish.bin.ConfScript;
import com.ms.service.module.fish.db.UsrGameLog;
import com.ms.service.module.fish.game.FishDesk;
import com.ms.service.module.fish.game.FishGame;
import com.ms.service.module.fish.game.FishGlobal;
import com.ms.service.module.fish.game.GameUser;
import com.ms.service.module.fish.message.FishMsgType;
import com.ms.service.module.fish.message.IWrite;
import com.ms.service.module.fish.message.server.AttackMessage;
import com.ms.service.module.fish.message.server.BatteryAttrMessage;
import com.ms.service.module.fish.message.server.CatchMessage;
import com.ms.service.module.fish.message.server.ChangeBatteryMessage;
import com.ms.service.module.fish.message.server.ChangeRateMessage;
import com.ms.service.module.fish.message.server.ConfsMessage;
import com.ms.service.module.fish.message.server.EnterGameMessage;
import com.ms.service.module.fish.message.server.EnterRoomMessage;
import com.ms.service.module.fish.message.server.FishMessage;
import com.ms.service.module.fish.message.server.GameInfoMessage;
import com.ms.service.module.fish.message.server.LabaMessage;
import com.ms.service.module.fish.message.server.LockAttackMessage;
import com.ms.service.module.fish.message.server.MultCatchMessage;
import com.ms.service.module.fish.message.server.NextStagMessage;
import com.ms.service.module.fish.message.server.OutGameMessage;
import com.ms.service.module.fish.message.server.RoomMessage;
import com.ms.service.module.fish.message.server.RoomRewardMessage;
import com.ms.service.module.fish.message.server.UpdateRoomMessage;
import com.ms.service.module.fish.message.server.UseBatteryMessage;
import com.ms.service.module.fish.message.server.UserEnterMessage;
import com.ms.service.module.fish.message.server.UserMessage;
import com.xpec.c4.game.utility.GUID;

public class SendUtil {

	public static void send100(MySession session,GameUser gameUser) {
		UserMessage um = new UserMessage(gameUser);
		doSend(session,FishMsgType.ACTION_100, um);
	}
	
	public static void send102(GameUser user) {
		RoomMessage um = new RoomMessage();
		doSend(user,FishMsgType.ACTION_102, um);
	}
	
	public static void send103(FishDesk desk,int index) {
		UpdateRoomMessage um = new UpdateRoomMessage(desk,index);
		Collection<MySession> sessions = FishGlobal.getUserSessions();
		for(MySession se:sessions) {
			GameUser seu = se.getUser();
			if(seu != null && seu.getDesk() == null) {
				// 大厅人员
				doSend(se,FishMsgType.ACTION_103, um);
				
			}
		}
	}
	
	public static void send110(MySession session,int desk, int index) {
		EnterRoomMessage um = new EnterRoomMessage(desk,index);
		doSend(session,FishMsgType.ACTION_110, um);
	}
	
	public static void send111(MySession session,GameUser gameUser) {
		EnterGameMessage um = new EnterGameMessage(gameUser);
		doSend(session,FishMsgType.ACTION_111, um);
	}
	
	public static void send112(GameUser gameUser) {
		UserEnterMessage um = new UserEnterMessage(gameUser);
		GameUser[] users = gameUser.getDesk().getUsers();
		for(GameUser ur:users){
			if(gameUser != ur){
				doSend(ur,FishMsgType.ACTION_112, um);
			}
		}
	}
	
	public static void send113(MySession session,GameUser gameUser) {
		GameInfoMessage um = new GameInfoMessage(gameUser);
		doSend(session,FishMsgType.ACTION_113, um);
	}
	
	public static void send118(FishDesk desk,GameUser gameUser) {
		if(desk == null) return;
		OutGameMessage um = new OutGameMessage(gameUser);
		GameUser[] users = desk.getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_118, um);
		}
		doSend(gameUser, FishMsgType.ACTION_118, um);
	}
	
	
	
	
	public static void send200(FishDesk desk,GameUser user, int battery,int lostCoin, int x, int y) {
		AttackMessage um = new AttackMessage(user,battery,lostCoin,x,y);
		GameUser[] users = desk.getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_200, um);
		}
	}
	
	public static void send201(FishDesk desk,ConfScript script) {
		FishMessage um = new FishMessage(script);
		GameUser[] users = desk.getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_201, um);
		}
	}
	
	public static void send202(FishDesk desk,int bullet,UsrGameLog log, GameUser user) {
		CatchMessage um = new CatchMessage(log,bullet,user);
		GameUser[] users = desk.getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_202, um);
		}
	}
	
	public static void send203(GameUser gameUser) {
		ChangeBatteryMessage um = new ChangeBatteryMessage(gameUser);
		GameUser[] users = gameUser.getDesk().getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_203, um);
		}
	}
	
	public static void send204(GameUser gameUser) {
		ChangeRateMessage um = new ChangeRateMessage(gameUser);
		GameUser[] users = gameUser.getDesk().getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_204, um);
		}
	}
	
	public static void send205(FishDesk desk, FishGame fishGame) {
		NextStagMessage um = new NextStagMessage(fishGame);
		GameUser[] users = desk.getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_205, um);
		}
	}
	
	public static void send206(FishDesk desk, GameUser gameUser,int lostCoin,int scriptId) {
		LockAttackMessage um = new LockAttackMessage(gameUser,lostCoin,scriptId);
		GameUser[] users = desk.getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_206, um);
		}
	}
	
	public static void send207(FishDesk desk,GameUser gameUser,int bullet, List<UsrGameLog> logs, GameUser user) {
		if(logs.isEmpty()) return;
		MultCatchMessage um = new MultCatchMessage(gameUser.getId().toString(), bullet,logs,user);
		GameUser[] users = desk.getUsers();
		for(GameUser ur:users){
			doSend(ur,FishMsgType.ACTION_207, um);
		}
	}
	
	public static void send208(GameUser gameUser,UsrGameLog log) {
		LabaMessage um = new LabaMessage(gameUser, log);
		doSend(gameUser, FishMsgType.ACTION_208, um);
	}
	
	public static void send209(MySession session,FishDesk desk) {
		RoomRewardMessage um = new RoomRewardMessage(desk);
		doSend(session, FishMsgType.ACTION_208, um);
	}
	
	public static void send210(MySession session,GameUser gameUser) {
		UseBatteryMessage um = new UseBatteryMessage(gameUser);
		if(session == null) {
			doSend(gameUser, FishMsgType.ACTION_210, um);
		}else {
			doSend(session, FishMsgType.ACTION_210, um);
		}
	}
	
	public static void send211(GameUser gameUser,int attr,int val) {
		BatteryAttrMessage um = new BatteryAttrMessage(attr,val);
		doSend(gameUser, FishMsgType.ACTION_211, um);
	}
	
	public static void send300(MySession session, int version) {
		ConfsMessage um = new ConfsMessage(version);
		doSend(session,FishMsgType.ACTION_300, um);
	}
	
	/**
	 * 发送信息
	 * @param sessionOrId 可以是session，gameUser,uid
	 * @param action
	 * @param data
	 */
	private static void doSend(Object sessionOrId,int action,IWrite data) {
		if(data == null)return;
		if(sessionOrId != null){
			MySession sess = null;
			if(sessionOrId instanceof MySession){
				sess = (MySession) sessionOrId;
			}else if(sessionOrId instanceof GameUser){
				sess = FishGlobal.getSessionByUid(((GameUser) sessionOrId).getId());
			}else if(sessionOrId instanceof GUID){
				sess = FishGlobal.getSessionBySid((GUID) sessionOrId);
			}
			if(sess != null){
				data.setCmd(action);
				sess.sendMessage(data);
			}
		}
	}
	

}
